package com.zj.base;

/**
 * @author zhangbo
 * @program: algorithm
 * @description: $
 * @date 2022-08-10 15:09:28
 */
public class BinarySearch {

    public int binarySearch(int[] arr, int num) {
        int l = 0;

        int r = arr.length - 1;

        int mid = (r - l) / 2 + l;

        int sub = -1;
        while (l < r) {
            if (arr[num] > arr[mid]) {
                l = mid + 1;
            } else if (arr[num] < arr[mid]){
                r = mid - 1;
            } else {
                sub = mid;
                return sub;
            }
            mid = (r - l) / 2 + l;
        }
        return sub;
    }
}
